package com.whg.demo.utils;

import com.itextpdf.text.DocumentException;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author whg
 * @create 2023/9/18 18:25
 * @description：
 */
public class FileIOUtils {

    public static void main(String[] args) throws DocumentException, IOException {
        File file = new File("D:\\\\home\\\\tonlan\\\\static\\\\printTemplate\\\\test.pdf");
        File outFile = new File("D:\\\\home\\\\tonlan\\\\static\\\\printTemplate\\\\test2.pdf");
        InputStream is = null;
        OutputStream os = null;
        try{
            is = new FileInputStream(file);
            //中文可能会乱码，建议仅当文件为txt文本文件时使用该方法（指定编码与文件的编码相同）
            String string = IOUtils.toString(is,"UTF-8");
            byte[] bytes = IOUtils.toByteArray(is);
            os = new FileOutputStream(outFile);
            //IOUtils.copy(is,os);
            //IOUtils.write(string,os,"UTF-8");
            //IOUtils.write(bytes,os);
            //FileUtils.copyFile(file,outFile);
            System.out.println(FilenameUtils.getExtension(file.getAbsolutePath()));
            System.out.println(FilenameUtils.getBaseName(file.getAbsolutePath()));
            System.out.println(FilenameUtils.getName(file.getAbsolutePath()));
            System.out.println(FilenameUtils.getFullPath(file.getAbsolutePath()));
            System.out.println(FilenameUtils.isExtension(file.getAbsolutePath(),
                    StringUtils.split("pdf,docx",",")));
            List<String> list = new ArrayList<>();
            list.add("123");
            list.add("124");
            list.add("125");
            list.add("126");
            System.out.println(org.springframework.util.StringUtils.collectionToDelimitedString(list,","));
        }catch (Exception e){

        }finally {
            //关闭一个IO流、socket、或者selector且不抛出异常，通常放在finally块
            IOUtils.closeQuietly(is);
            IOUtils.closeQuietly(os);
            //FileUtils.deleteQuietly(file);
        }
    }
}
